<?php
/**
 * Remove a document from a mission
 * 
 * @package Hive\Mission
 */

use Fox\Generator;
use Fox\Core\Errors;
$xview = Xview::instance();
$id = $xview->val("missionid");
$documentid = $xview->val("docid");
if(empty($documentid))
{
	$xview->systemError();exit;
}
try {
	$object = new Hive\Mission($id);
	$document = new Fox\Document($documentid);
}
catch (Fox\Object\Exception\NotExist $e)
{
	$xview->systemError();exit;
}
if($xview->val("confirmation"))
{
	try {
		$object->deleteFromDocuments(\Fox\Dao::load(Hive\Mission\Document::className(),"$id:$documentid"));
		$object->prepareUpdate(Hive\Ressource::CHANGE_DOCUMENT_DELETE);
		$object->update();
		$document->delete();
		echo Fox\Message::Information(Msg(Hive::BUNDLE,Hive::DOCUMENT_DELETED));
		exit;
	}
	catch (Fox\Object\Exception\Save $e)
	{
		Errors::pushMessage($e);
	}
}

$reloadGen = Generator::ajaxReload("contentCol",Generator::url("missions","documents",null,array("mid" => $id)));
$yesAction = Generator::ajax(Generator::url('missions','documents','delete',array('missionid' => $id,'docid' => $documentid )),
							'&confirmation=true',
							$reloadGen,'hdeleteForm',null,2
);
//$reload = Generator::generateValue(Generator::ajaxReload('\'MC_documents\''),'');
//$reload = "Reload(\'MC_documents\')";
//$yesAction = "FoxPostForm('deleteDoc','?mod=ressource&act=documents&sac=delete&docid=$documentid&missionid=$id','$reload',null,null,2,'&confirmation=true')";

$popup = new Fox\Popup\ToConfirm("deleteDoc",Msg(Hive::BUNDLE,7), Msg(Hive::BUNDLE,6,$document->filename),$yesAction,null,400);
echo $popup->Display();